package com.foredu.fiqs.web.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.domain.Role;
import com.foredu.fiqs.domain.Teacher;
import com.foredu.fiqs.domain.User;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.TeacherActionForm;

public class TeacherAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到教师管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获得当前登录用户
		User user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		if (user.getRoles().contains(role)) {
			Teacher teacher = managementService.findTeacherByUserId(user
					.getId());
			List<Teacher> teacherList = managementService
					.findTeachersOfDept(teacher.getDept());
			request.setAttribute("teacherList", teacherList);
		} else {
			List<Teacher> teacherList = managementService.findTeachers();
			request.setAttribute("teacherList", teacherList);
		}

		return mapping.findForward("list");
	}

	// 跳转到教师添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		User user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		if (user.getRoles().contains(role)) {
			Teacher teacher = managementService.findTeacherById(user.getId());
			Department department = managementService
					.findDepartmentById(teacher.getDept().getId());
			request.setAttribute("department", department);

		} else {
			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);
		}

		return mapping.findForward("add");
	}

	// 完成到教师添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherActionForm taf = (TeacherActionForm) form;

		// 判断当前输入的用户名是否合法！
		User userOfTeacher = managementService.findUserByUsername(taf
				.getUsername());
		if (userOfTeacher != null) {
			request.setAttribute("userExistError", "此用户名已注册，请使用其他用户名！");

			return mapping.findForward("toAdd");
		}

		// 表单校验
		if (taf.getUsername() == null || "".equals(taf.getUsername())) {
			request.setAttribute("usernameError", "请输入用户名!");

			return mapping.findForward("toAdd");
		}
		if (taf.getPassword() == null || "".equals(taf.getPassword())) {
			request.setAttribute("passwordError", "请输入密码！");

			return mapping.findForward("toAdd");
		}

		if (taf.getTeachId() == null || "".equals(taf.getTeachId())) {
			request.setAttribute("teachIdError", "请输入教师编号！");

			return mapping.findForward("toAdd");
		} else if (managementService.findTeacherByTeachId(taf.getTeachId()) != null) {
			request.setAttribute("teachIdRequest", "该教师编号已存在，请重新输入");

			return mapping.findForward("toAdd");
		}

		if (taf.getNickname() == null || "".equals(taf.getNickname())) {
			request.setAttribute("nicknameError", "请输入教师姓名！");

			return mapping.findForward("toAdd");
		}
		if (taf.getProfessional() == null || "0".equals(taf.getProfessional())) {
			request.setAttribute("prifessionalError", "请选择教师职称！");

			return mapping.findForward("toAdd");
		}
		if (taf.getDeptId() == 0) {
			request.setAttribute("deptError", "请选择所属院系！");

			return mapping.findForward("toAdd");
		}

		if (taf.getType() == 0) {
			request.setAttribute("typeError", "请选择教师类型！");

			return mapping.findForward("toAdd");
		}
		
		Teacher teacher = new Teacher();
		PropertyUtils.copyProperties(teacher, taf);

		Department dept = managementService.findDepartmentById(taf.getDeptId());
		teacher.setDept(dept);

		// 设置教师用户的角色为"USER"和"TEACHER"
		Role roleOfUser = managementService.findRoleByName("USER");
		Role roleOfTeacher = managementService.findRoleByName("TEACHER");
		Set<Role> roles = new HashSet<Role>();
		roles.add(roleOfUser);
		roles.add(roleOfTeacher);
		teacher.setRoles(roles);

		managementService.saveTeacher(teacher);

		return mapping.findForward("save");
	}

	// 跳转到教师修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherActionForm taf = (TeacherActionForm) form;
		Teacher teacher = managementService.findTeacherById(taf.getUserId());

		request.setAttribute("teacher", teacher);

		// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
		taf.setDeptId(teacher.getDept().getId());
		taf.setProfessional(teacher.getProfessional());

		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		Department department = managementService.findDepartmentById(taf
				.getDeptId());
		request.setAttribute("department", department);

		return mapping.findForward("update");
	}

	// 保存教师修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherActionForm taf = (TeacherActionForm) form;

		// 表单校验
		if (!taf.getTeachId().equals(managementService.findTeacherById(taf.getUserId()).getTeachId())) {
			if (taf.getTeachId() == null || "".equals(taf.getTeachId())) {
				request.setAttribute("teachIdError", "请输入教师编号！");

				Teacher teacher = managementService.findTeacherById(taf.getUserId());

				request.setAttribute("teacher", teacher);

				// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
				taf.setDeptId(teacher.getDept().getId());
				taf.setProfessional(teacher.getProfessional());

				Department department = managementService.findDepartmentById(taf.getDeptId());
				request.setAttribute("department", department);

				List<Department> deptList = managementService.findDepartments();
				request.setAttribute("deptList", deptList);

				return mapping.findForward("update");
			} else if (managementService.findTeacherByTeachId(taf.getTeachId()) != null) {

				request.setAttribute("teachIdRequest", "该教师编号已存在，请重新输入");

				Teacher teacher = managementService.findTeacherById(taf
						.getUserId());

				request.setAttribute("teacher", teacher);

				// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
				taf.setDeptId(teacher.getDept().getId());
				taf.setProfessional(teacher.getProfessional());

				List<Department> deptList = managementService.findDepartments();
				request.setAttribute("deptList", deptList);

				return mapping.findForward("update");
			}
		}

		if (taf.getNickname() == null || "".equals(taf.getNickname())) {
			request.setAttribute("nicknameError", "请输入教师姓名！");

			Teacher teacher = managementService
					.findTeacherById(taf.getUserId());

			request.setAttribute("teacher", teacher);

			// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
			taf.setDeptId(teacher.getDept().getId());
			taf.setProfessional(teacher.getProfessional());

			Department department = managementService.findDepartmentById(taf
					.getDeptId());
			request.setAttribute("department", department);

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("update");
		}

		if (taf.getProfessional() == null || "0".equals(taf.getProfessional())) {
			request.setAttribute("prifessionalError", "请选择教师职称！");

			Teacher teacher = managementService
					.findTeacherById(taf.getUserId());

			request.setAttribute("teacher", teacher);

			// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
			taf.setDeptId(teacher.getDept().getId());
			taf.setProfessional(teacher.getProfessional());

			Department department = managementService.findDepartmentById(taf
					.getDeptId());
			request.setAttribute("department", department);

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("update");
		}

		if (taf.getDeptId() == 0) {
			request.setAttribute("deptError", "请选择所属院系！");

			Teacher teacher = managementService
					.findTeacherById(taf.getUserId());

			request.setAttribute("teacher", teacher);

			// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
			taf.setDeptId(teacher.getDept().getId());
			taf.setProfessional(teacher.getProfessional());

			Department department = managementService.findDepartmentById(taf
					.getDeptId());
			request.setAttribute("department", department);

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("update");
		}

		if (taf.getType() == 0) {
			request.setAttribute("typeError", "请选择教师类型！");

			Teacher teacher = managementService
					.findTeacherById(taf.getUserId());

			request.setAttribute("teacher", teacher);

			// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
			taf.setDeptId(teacher.getDept().getId());
			taf.setProfessional(teacher.getProfessional());

			Department department = managementService.findDepartmentById(taf
					.getDeptId());
			request.setAttribute("department", department);

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("update");
		}
		
		Teacher teacher = managementService.findTeacherById(taf.getUserId());
		PropertyUtils.copyProperties(teacher, taf);

		Department dept = managementService.findDepartmentById(taf.getDeptId());
		teacher.setDept(dept);

		managementService.updateTeacher(teacher);

		return mapping.findForward("updateSave");
	}

	// 完成教师删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TeacherActionForm taf = (TeacherActionForm) form;

		managementService.deleteTeacherById(taf.getUserId());

		return mapping.findForward("delete");
	}
}
